﻿package mortal.game.view.mainUI.smallMap.view.render
{
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.view.common.*;
    import mortal.game.view.mainUI.smallMap.view.data.*;
    import mortal.mvc.core.*;

    public class SmallMapWorldRegionItem extends GSprite
    {
        public var _selectedCallback:Function;
        private var _bm:GBitmap;
        private var _data:SmallMapWorldRegionData;
        private var _bmpName:GImageBitmap;
        private var _bmpNameBg:GImageBitmap;
        private var _selfIcon:GBitmap;
        private var _isLvEnough:Boolean = true;
        private var _lv:int = 1;
        private var _enabledFilter:Array;
        private static var _point:Point = new Point(0, 0);
        private static var _point2:Point = new Point(0, 0);

        public function SmallMapWorldRegionItem()
        {
            this._enabledFilter = [];
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseChildren = true;
            this.mouseEnabled = true;
            this.buttonMode = true;
            this._bm = UIFactory.gBitmap("", 0, 0, this);
            var _loc_1:* = GlobalStyle.textFormatHuang2;
            _loc_1.size = 18;
            _loc_1.font = FontUtil.lishuName;
            _loc_1.align = TextFormatAlign.CENTER;
            this._bmpNameBg = UIFactory.gImageBitmap(null, 30, 30, this);
            this._bmpName = UIFactory.gImageBitmap(null, this._bmpNameBg.x + 18, this._bmpNameBg.y + 16, this);
            this.configEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);
            this.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOutHandler);
            this.configEventListener(MouseEvent.CLICK, this.clickMeHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._bm.dispose(param1);
            this._bm = null;
            this._isLvEnough = true;
            this._bmpName.dispose(param1);
            this._bmpName = null;
            this._bmpNameBg.dispose(param1);
            this._bmpNameBg = null;
            this.disposeSelfIcon();
            return;
        }// end function

        private function disposeSelfIcon() : void
        {
            if (this._selfIcon != null)
            {
                this._selfIcon.dispose();
                this._selfIcon = null;
            }
            return;
        }// end function

        public function update(param1:SmallMapWorldRegionData) : void
        {
            var _loc_4:int = 0;
            this._data = param1;
            if (param1.isRegion)
            {
                LoaderManager.instance.load(param1.url + ".png", this.resGotHandler);
            }
            else
            {
                LoaderHelp.addResCallBack(param1.url, this.resGotHandler2);
            }
            this.x = param1.x;
            this.y = param1.y;
            var _loc_2:int = 1;
            var _loc_3:* = SceneConfig.instance.getSceneInfo(this._data.mapId);
            if (_loc_3 != null)
            {
                _loc_2 = _loc_3.sMapDefine.needLevel;
            }
            this._isLvEnough = true;
            _loc_2 = Math.max(_loc_2, this._data.enterLevel);
            this._lv = _loc_2;
            if (_loc_2 > Cache.instance.role.roleInfo.level || !param1.opened)
            {
                this._enabledFilter = [FilterConst.colorFilter];
                this._isLvEnough = false;
                LoaderHelp.setBitmapdata("MapNameBg_0.png", this._bmpNameBg);
                LoaderHelp.setBitmapdata("MapName" + this._data.mapId + "_0.png", this._bmpName);
                this.disposeSelfIcon();
            }
            else
            {
                this._enabledFilter = [];
                _loc_4 = -1;
                if (Game.mapInfo != null)
                {
                    _loc_4 = Game.mapInfo.mapId;
                }
                if (_loc_4 != this._data.mapId)
                {
                    LoaderHelp.setBitmapdata("MapNameBg_1.png", this._bmpNameBg);
                    LoaderHelp.setBitmapdata("MapName" + this._data.mapId + "_1.png", this._bmpName);
                    this.disposeSelfIcon();
                }
                else
                {
                    LoaderHelp.setBitmapdata("MapNameBg_2.png", this._bmpNameBg);
                    LoaderHelp.setBitmapdata("MapName" + this._data.mapId + "_2.png", this._bmpName);
                    if (this._selfIcon == null)
                    {
                        this._selfIcon = UIFactory.gBitmap(ImagesConst.MapPoint_Self2, this._bmpNameBg.x + 78, this._bmpNameBg.y + 16, this);
                    }
                    this.addChild(this._selfIcon);
                }
            }
            this.filters = this._enabledFilter;
            this._bmpNameBg.x = this._data.namex;
            this._bmpNameBg.y = this._data.namey;
            this._bmpName.x = this._bmpNameBg.x + 18;
            this._bmpName.y = this._bmpNameBg.y + 16;
            return;
        }// end function

        public function clearSelected() : void
        {
            if (this._bm == null)
            {
                return;
            }
            if (this._bm.filters == this._enabledFilter)
            {
                return;
            }
            this._bm.filters = this._enabledFilter;
            return;
        }// end function

        private function resGotHandler2() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._bm.bitmapData = GlobalClass.getBitmapData("Map_" + this._data.mapId);
            return;
        }// end function

        private function resGotHandler(param1:ImageInfo) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._bm.bitmapData = param1.bitmapData;
            return;
        }// end function

        private function mouseOverHandler(event:MouseEvent) : void
        {
            this.mouseMoveHandler(null);
            return;
        }// end function

        private function mouseMoveHandler(event:MouseEvent) : void
        {
            if (this._bm == null || this._bm.bitmapData == null)
            {
                return;
            }
            _point2.x = this.mouseX;
            _point2.y = this.mouseY;
            if (!this._bm.bitmapData.hitTest(_point, 0, _point2))
            {
                this._bm.filters = this._enabledFilter;
            }
            else
            {
                if (this._selectedCallback != null)
                {
                    this._selectedCallback.apply();
                }
                this._bm.filters = [FilterConst.colorGlowFilter(16776960)].concat(this._enabledFilter);
            }
            return;
        }// end function

        private function mouseOutHandler(event:MouseEvent) : void
        {
            this.mouseMoveHandler(null);
            return;
        }// end function

        private function clickMeHandler(event:MouseEvent) : void
        {
            if (this._bm == null || this._bm.bitmapData == null)
            {
                return;
            }
            _point2.x = this._bm.mouseX;
            _point2.y = this._bm.mouseY;
            if (!this._bm.bitmapData.hitTest(_point, 2, _point2))
            {
                return;
            }
            if (!this._data.opened)
            {
                MsgManager.showRollTipsMsg(Language.getString(21275));
                return;
            }
            if (!this._isLvEnough)
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(21276, this._lv));
                return;
            }
            if (this._enabledFilter.length > 0)
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmallMapClickWorldRegion, this._data));
            return;
        }// end function

    }
}
